package cn.maihe.elg.operation.centers.guogu.channel.service;

import cn.maihe.elg.operation.centers.guogu.channel.GuoGuBaseChannelService;
import cn.maihe.elg.operation.centers.guogu.dto.GuoGuBaseRespDTO;
import cn.maihe.elg.operation.centers.guogu.dto.channel.req.GuoGuInvoiceNotifyReq;
import cn.maihe.elg.operation.centers.guogu.enums.GuoGuChannelMethod;
import cn.maihe.elg.operation.centers.guogu.enums.GuoGuRespCodeEnum;
import cn.maihe.elg.operation.model.dto.InnerBaseResp;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.InvoiceFlagEnum;
import cn.maihe.elg.operation.model.enums.InvoiceNotifyStatusEnum;
import cn.maihe.elg.operation.model.enums.InvoiceStatusEnum;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.InvoiceInfoDO;
import cn.maihe.elg.operation.service.invoice.InvoiceInfoService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.utils.ElgLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Description 开票结果通知
 * @Author SSX
 * @Date 2025/03/18
 */
@Service
@Slf4j
public class GuoGuInvoiceNotifyChannelService extends GuoGuBaseChannelService<GuoGuInvoiceNotifyReq, GuoGuBaseRespDTO> {
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private InvoiceInfoService invoiceInfoService;
    @Override
    protected GuoGuChannelMethod methodEnum() {
        return GuoGuChannelMethod.invoice_notify;
    }

    /**
     * 开票结果通知
     */
    public InnerBaseResp<GuoGuBaseRespDTO> invoiceResultNotify(String acceptOrderNo) {
        AcceptOrderInfoDO acceptOrderInfoDO = acceptOrderInfoService.getByAcceptOrderNo(acceptOrderNo);
        if(Objects.isNull(acceptOrderInfoDO)){
            return InnerBaseResp.error(null,"查无此受理订单信息");
        }

        InvoiceInfoDO invoiceInfoDO = invoiceInfoService.selectById(acceptOrderInfoDO.getInvoiceId());
        if(Objects.isNull(invoiceInfoDO)){
            return InnerBaseResp.error(null,"查无此发票信息");
        }

        if(InvoiceNotifyStatusEnum.SUCCESS.getNotifyStatus() == invoiceInfoDO.getNotifyStatus()){
            log.warn("开票结果通知>> 已成功通知，无需再通知");
            return InnerBaseResp.success(null,"已成功通知，无需再通知");
        }

        if (StringUtils.isBlank(invoiceInfoDO.getInvoiceNo()) ||
                invoiceInfoDO.getStatus() != InvoiceStatusEnum.INVOICED.getValue()) {
            ElgLogger.info(log, log -> log.info("受理订单号[{}] << 当前开票状态为[{}]稍后再试", acceptOrderInfoDO.getAcceptOrderNo(),InvoiceStatusEnum.getLabel(invoiceInfoDO.getStatus()) ));
            return InnerBaseResp.error(null,"未开票，稍后再试");
        }

        // 开票结果通知
        GuoGuInvoiceNotifyReq guoGuInvoiceNotifyReq = new GuoGuInvoiceNotifyReq();
        guoGuInvoiceNotifyReq.setResult("1");
        guoGuInvoiceNotifyReq.setTradeNo(acceptOrderInfoDO.getCenterOrderId());
        guoGuInvoiceNotifyReq.setDownloadUrl(invoiceInfoDO.getPdfUrl());
        guoGuInvoiceNotifyReq.setMsg("开票成功");
        guoGuInvoiceNotifyReq.setOnlineUrl(invoiceInfoDO.getPdfUrl());

        CenterNoEnum centerNoEnum = this.getCenterNoEnumById(acceptOrderInfoDO.getCenterId());

        InvoiceNotifyStatusEnum notifyStatus;
        InnerBaseResp<GuoGuBaseRespDTO> innerBaseResp;
        try {
            GuoGuBaseRespDTO resp = this.doRequest(acceptOrderInfoDO.getClientId(), centerNoEnum, guoGuInvoiceNotifyReq);
            if (GuoGuRespCodeEnum.isSuccess(resp.getCode())) {
                notifyStatus = InvoiceNotifyStatusEnum.SUCCESS;
                innerBaseResp = InnerBaseResp.success(guoGuInvoiceNotifyReq.getTradeNo());
            } else {
                notifyStatus = InvoiceNotifyStatusEnum.FAILURE;
                innerBaseResp = InnerBaseResp.error(resp.getMsg());
            }
        } catch (Exception e) {
            notifyStatus = InvoiceNotifyStatusEnum.FAILURE;
            log.error("开票结果通知>> [{}] 异常:{}", centerNoEnum.getCenterName(), e.getMessage(), e);
            innerBaseResp = InnerBaseResp.error("通知异常:" + e.getMessage());
        }

        InvoiceInfoDO updateNotifyStatus = new InvoiceInfoDO();
        updateNotifyStatus.setId(invoiceInfoDO.getId());
        updateNotifyStatus.setNotifyStatus(notifyStatus.getNotifyStatus());
        invoiceInfoService.updateById(updateNotifyStatus);

        AcceptOrderInfoDO updateAcceptOrder = AcceptOrderInfoDO.builder().id(acceptOrderInfoDO.getId()).build();
        updateAcceptOrder.setInvoiceFlag(InvoiceFlagEnum.INVOICED.getValue());
        updateAcceptOrder.setInvoiceNo(invoiceInfoDO.getInvoiceNo());
        acceptOrderInfoService.updateById(updateAcceptOrder);
        return innerBaseResp;
    }
}
